package cn.lyjuan.android.demofive;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import cn.lyjuan.android.demofive.chapter12.app_12_2_5_1.TumblerActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by chad on 16-1-11.
 */
public class ListItemClickListener implements AdapterView.OnItemClickListener
{
    ArrayList<Res.IItem> items;

    public ListItemClickListener(ArrayList<Res.IItem> items)
    {
        this.items = items;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        Context context = view.getContext();
        Intent intent = null;
        Res.IItem item = items.get(position);
        String title = item.getName();

        // tag
        if (item instanceof Res.TagInfo)
        {
            Res.TagInfo tagInfo = (Res.TagInfo) item;
            if (null == tagInfo.items || tagInfo.items.isEmpty())
            {
                Toast.makeText(context, "tag下无内容", Toast.LENGTH_SHORT).show();
                return ;
            }

            intent = new Intent(context, TagActivity.class);
            intent.putExtra(TagActivity.EXTRA_ITEMS, tagInfo.items);
        } else if (item instanceof Res.ActivityInfo)
        {// activity
            Res.ActivityInfo activityInfo = (Res.ActivityInfo) item;

            if (null == activityInfo.cls)
            {
                Toast.makeText(context, "该item无相应的 Class", Toast.LENGTH_SHORT).show();
                return ;
            }

            intent = new Intent(context, activityInfo.cls);
        } else
        {
            Toast.makeText(context, "该item即无tag也无class", Toast.LENGTH_SHORT).show();
            return ;
        }


        intent.putExtra(TagActivity.EXTRA_TITLE, title);

        context.startActivity(intent);
    }
}
